package com.example.iot.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.iot.dao.RuleNodeDao;
import com.example.iot.entity.id.RuleChainId;
import com.example.iot.entity.id.RuleNodeId;
import com.example.iot.entity.po.RuleNodePO;
import com.example.iot.mapper.mysql.RuleNodeMapper;
import com.example.iot.dao.BaseDaoImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author ChenXiangpeng
 */
@Service
public class RuleNodeDaoImpl extends BaseDaoImpl<RuleNodeMapper, RuleNodePO> implements RuleNodeDao {
    @Override
    public void deleteRuleNode(RuleNodeId ruleNodeId) {
        this.baseMapper.deleteById(ruleNodeId.getIdStr());
    }

    @Override
    public Optional<RuleNodePO> findRuleNodeById(RuleNodeId ruleNodeId) {
        RuleNodePO ruleNode = this.baseMapper.selectById(ruleNodeId.getIdStr());
        return Objects.isNull(ruleNode) ? Optional.empty() : Optional.of(ruleNode);
    }

    @Override
    public List<RuleNodePO> getRuleChainNodes(RuleChainId ruleChainId) {
        QueryWrapper<RuleNodePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(RuleNodePO.RULE_CHAIN_ID, ruleChainId.getIdStr());
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteByChainId(RuleChainId ruleChainId) {
        QueryWrapper<RuleNodePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(RuleNodePO.RULE_CHAIN_ID, ruleChainId.getIdStr());
        this.baseMapper.delete(queryWrapper);
    }
}
